package test;

import java.sql.DriverManager;
import java.sql.Connection;

public class MySqlCon {
	public static void main(String[] args) throws Exception {
		String url="jdbc:mysql://localhost:3306/mydemo?useUnicode=true&characterEncoding=UTF-8&user=root&password=root";
		Connection con = DriverManager.getConnection(url);
        // 加载驱动
        // 连接数据库的地址
        // String url = "jdbc:odbc:database";
        //String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=database.mdb";

        Connection conn = null;
        try {
            // 创建与数据库的连接
            conn = DriverManager.getConnection(url);
            System.out.println("成功连接到数据库：" + conn);
        } catch(Exception ex) {
            System.out.println("连接失败：" + ex);
        } finally {
            // 关闭连接，释放资源
            if (conn != null) {
                conn.close();
            }
        }
    }
}
